import java.util.ArrayList;
import java.util.List;

//组合
//测试链接：https://leetcode.cn/problems/combinations/
public class Combine {
    List<List<Integer>> ans;
    List<Integer> path;
    public List<List<Integer>> combine(int n, int k) {
        ans = new ArrayList<>();
        path = new ArrayList<>();
        dfs(n,k,1);
        return ans;
    }

    public void dfs(int n, int k, int pos) {
        if(path.size() == k) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for(int i = pos; i <= n; i++) {
            path.add(i);
            dfs(n,k,i+1);
            path.remove(path.size()-1); //恢复现场

        }
    }
}
